package com.dhh.toodledo.activity;

import java.util.Timer;
import java.util.TimerTask;
import com.dhh.toodledo.R;
import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.util.AuthUtil;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

public class NavigateActivity extends Activity{
	private AuthUtil au;
	private SharedPreferences sp;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.startup);
		sp = getSharedPreferences("myPre", 0);
        au = new AuthUtil(sp); 
	}
	
	public void onResume(){
		super.onResume();
		Timer timer = new Timer();
		timer.schedule(timerTask, 2000);
	}

	TimerTask timerTask = new TimerTask() {
		@Override
		public void run() {
			try {
				au.getKeyValue();
				handler.sendEmptyMessage(0);
			} catch (InValidAccountException e) {
				eHandler.sendEmptyMessage(0);
			}

		}
	};
	 
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(NavigateActivity.this,TDAActivity.class);
        	NavigateActivity.this.startActivity(intent);
        }
    };
    
    private Handler eHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(NavigateActivity.this,LoginActivity.class);
        	NavigateActivity.this.startActivity(intent);
        }
    };
}
